package de.tc.stammdatenverwaltung.printing;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JOptionPane;

public class druckeKontakt {

  public static void starteDruck(String[] values)
  {
    // Create an object that will hold all print parameters, such as
    // page size, printer resolution. In addition, it manages the print
    // process (job).
    PrinterJob job = PrinterJob.getPrinterJob();

    // It is first called to tell it what object will print each page.
    job.setPrintable(new PrintObject(values));

    // Then it is called to display the standard print options dialog.
    if (job.printDialog())
      {
         // If the user has pressed OK (printDialog returns true), then go
         // ahead with the printing. This is started by the simple call to
         // the job print() method. When it runs, it calls the page print
         // object for page index 0. Then page index 1, 2, and so on
         // until NO_SUCH_PAGE is returned.
         try { job.print(); }
         catch (PrinterException e)
         {
           JOptionPane.showMessageDialog(null, "Fehler beim drucken:\r\n" + e.getLocalizedMessage(),
                "Fehler!", 1);
           e.printStackTrace();
         }
      }

  }

}

class PrintObject implements Printable
{

  String[] printingValues = null;
  String[] printingTitles = new String[]{"Anrede", "Titel", "Vorname", "2. Name", "Name", "Spitzname",
    "Anschrift", "PLZ/Ort", "Telefon", "Fax", "Mobil", "E-Mail1", "E-Mail2",
    "Firma", "Position", "Beruf", "Anschrift", "PLZ/Ort", "Telefon", "Fax", "E-Mail",
    "Geburtstag", "Markierung", "Alumnus:", "Abitur", "Ämter",
      "Forum Thomanum", "Thomanerbund", "Förderkreis Thomanerchor", "Freund des Chores"};
  public PrintObject(String[] values)
  {
    printingValues = values;
  }

  public int print (Graphics g, PageFormat f, int pageIndex)
  {
    Graphics2D graph2d = (Graphics2D) g;  // Allow use of Java 2 graphics on the print pages :

    float spalteLinksX = (float) f.getImageableX() + 10;
    float spalteRechtsX = (float) f.getImageableX() + 10 + 100;
    float yPos = (float) f.getImageableY() + 20;

    float outerBoxY = 0, innerBoxY = 0;

    for (int i = 0; i < printingValues.length; i++)
    {
      if (i == 0) //Privat, name
      {
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 14));
        graph2d.drawString("Privat", spalteLinksX, yPos);
        outerBoxY = yPos + 2;
        spalteLinksX += 10; yPos += 15;

        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 12));
        graph2d.drawString("Name", spalteLinksX, yPos);
        innerBoxY = yPos + 2;
        spalteLinksX += 10; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }
      if (i == 6) //privat, kontakt
      {
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 12));
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);
        graph2d.drawString("Kontakt", spalteLinksX, yPos);
        innerBoxY = yPos + 3;
        spalteLinksX += 10; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }

      if (i == 13) //geschäftlich, allgemein
      {
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);
        
        if (f.getOrientation() == f.PORTRAIT)
          yPos += 10;

        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 14));
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) outerBoxY, 420, (int) (yPos - outerBoxY) - 12);

        graph2d.drawString("Geschäftlich", spalteLinksX, yPos);
        outerBoxY = yPos + 2;
        spalteLinksX += 10; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 12));
        graph2d.drawString("Allgemein", spalteLinksX, yPos);
        innerBoxY = yPos + 3;
        spalteLinksX += 10; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }
      if (i == 16) //geschäftlich, kontakt
      {
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 12));
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);
        graph2d.drawString("Kontakt", spalteLinksX, yPos);
        innerBoxY = yPos + 3;
        spalteLinksX += 10; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }
      if (i == 20) //sonstiges
      {
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);

        if (f.getOrientation() == f.PORTRAIT)
          yPos += 10;
        
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 14));
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) outerBoxY, 420, (int) (yPos - outerBoxY) - 12);
        graph2d.drawString("Sonstiges", spalteLinksX, yPos);
        outerBoxY = yPos + 2;
        innerBoxY = yPos + 3;
        spalteLinksX += 20; yPos += 15;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }
      if (i == 24) //privat, kontakt
      {
        graph2d.setFont(new Font("Times New Roman", Font.BOLD, 12));
        spalteLinksX -= 10;
        graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);
        graph2d.drawString("Vereine", spalteLinksX, yPos);
        innerBoxY = yPos + 3;
        spalteLinksX += 10; yPos += 15;
        spalteRechtsX += 40;
        graph2d.setFont(new Font("Times New Roman", Font.PLAIN, 10));
      }
      graph2d.drawString(printingTitles[i], spalteLinksX, yPos);
      graph2d.drawString(printingValues[i], spalteRechtsX, yPos);

      yPos += 15;
    }
    //abschließende Boxen
    spalteLinksX -= 10;
    graph2d.drawRect((int) spalteLinksX, (int) innerBoxY, 400, (int) (yPos - innerBoxY) - 12);
    spalteLinksX -= 10;
    graph2d.drawRect((int) spalteLinksX, (int) outerBoxY, 420, (int) (yPos - outerBoxY) - 10);

    //seitenrand
    graph2d.drawRect( (int) f.getImageableX(), (int) f.getImageableY(),
            (int) f.getImageableWidth() - 1, (int) f.getImageableHeight() - 1);

    switch (pageIndex) //der Sinn erschließt sich mir nicht, besser lassen
    {
      case 0 : return PAGE_EXISTS;
      default: return NO_SUCH_PAGE;
    }
  }
}